package com.hanson.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanson.entity.*;
import com.hanson.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Configuration
@EnableScheduling
@Slf4j
public class SchedulingConfig {

    @Autowired
    private GoodsBeanMapper goodsBeanMapper;
    @Autowired
    private FileBeanMapper fileBeanMapper;
    @Autowired
    private GoodsTypeBeanMapper goodsTypeBeanMapper;
    @Autowired
    private GoodsContentBeanMapper goodsContentBeanMapper;
    @Autowired
    private GoodsAttrValueBeanMapper goodsAttrValueBeanMapper;

    //@PostConstruct
    public void init() {
        List<GoodsBean> goodsBeanList = goodsBeanMapper.selectAll();
        if (CollectionUtils.isNotEmpty(goodsBeanList)) {
            goodsBeanList.stream().forEach(c -> {
                c.setImage(splitImage(c.getImage()));
                c.setSliderImage(splitImage(c.getSliderImage()));
                goodsBeanMapper.updateByPrimaryKey(c);
            });
        }
        List<FileBean> fileBeanList = fileBeanMapper.selectAll();
        if (CollectionUtils.isNotEmpty(fileBeanList)) {
            fileBeanList.stream().forEach(c -> {
                c.setSfilePath(splitImage(c.getSfilePath()));
                c.setFilePath(splitImage(c.getFilePath()));
                fileBeanMapper.updateByPrimaryKey(c);
            });
        }

        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeBeanMapper.selectAll();
        if (CollectionUtils.isNotEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                c.setPic(splitImage(c.getPic()));
                goodsTypeBeanMapper.updateByPrimaryKey(c);
            });
        }

        List<GoodsContentBean> goodsContentBeanList = goodsContentBeanMapper.selectAll();
        if(CollectionUtils.isNotEmpty(goodsContentBeanList)){
            goodsContentBeanList.stream().forEach(c->{
                c.setContent(repairContent(c.getContent()));
                goodsContentBeanMapper.updateByPrimaryKey(c);
            });
        }

        List<GoodsAttrValueBean> goodsAttrValueBeanList = goodsAttrValueBeanMapper.selectAll();
        if(CollectionUtils.isNotEmpty(goodsAttrValueBeanList)){
            goodsAttrValueBeanList.stream().forEach(c->{
                c.setImage(splitImage(c.getImage()));
                JSONObject jsonObject = (JSONObject) JSONObject.parse(c.getDetail());
                String image = jsonObject.getString("image");
                if(StringUtils.isNotEmpty(image)){
                    image = splitImage(image);
                    jsonObject.put("image",image);
                    c.setDetail(JSON.toJSONString(jsonObject));
                }
                goodsAttrValueBeanMapper.updateByPrimaryKey(c);
            });
        }
    }

    public static String repairContent(String content) {
        String patternStr = "<img\\s*([^>]*)\\s*src=\\\"(.*?)\\\"\\s*([^>]*)>";
        Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        String result = content;
        while (matcher.find()) {
            String src = matcher.group(2);
            log.debug("pattern string:" + src);
            String[] array = src.split("\\?");
            if (array.length == 2) {
                result = result.replace(src, array[0]);
            }
        }
        return result;
    }


    public static String splitImage(String images) {
        if (StringUtils.isEmpty(images)) {
            return null;
        }
        String[] imageArray = images.split("\\,");
        StringBuilder sb1 = new StringBuilder();
        for (String image : imageArray) {
            sb1.append(image.split("\\?")[0]).append(",");
        }
        return sb1.substring(0, sb1.length() - 1).toString();
    }
}
